<?php

include 'logic_layer.php';


if (!isset($_SESSION)) {
    session_start();
}

//--------
//--------
if (isset($_SESSION["step"])) {
    $current_step = $_SESSION['step'];
} else {
    $current_step = 'welcome';
}

//create a new pid prefix for pilot study
if (isset($_GET['pilotmode']) && !isset($_SESSION['pid_prefix'])) {
    if ($_GET['pilotmode'] == 'on') {
        $_SESSION['pid_prefix'] = 'PILOT_';
    } else {
        $_SESSION['pid_prefix'] = '';
    }
}

if (isset($_POST["step"])) {
    $next_step = $_POST["step"];
} else {
    //use the session step
    $next_step = $current_step;
}


//persistent
//--------
//--------
if ($next_step != $current_step) {
    //welcome
    if ($current_step == "welcome") {
        if (isset($_POST['email'])) {
            $_SESSION['email'] = $_POST['email'];
        }
        //set a unique participant id
        $_SESSION['pid'] = createParticipantID();
        //get IP
        $_SESSION['ip'] = getClientIP();
        //save the participant
        savePaticipant();
    }
    //personality test
    else if ($current_step == "pt") {
        savePT($_POST);
    }
    //creating an recipe
    else if ($current_step == "create") {
        saveRecipe($_POST);
    }
    //intrinsic motivation inventory questionnaire
    else if ($current_step == "imiq") {
        saveIMI($_POST);
    }
    //intrinsic motivation inventory questionnaire
    else if ($current_step == "motivation") {
        saveMotivation($_POST);
    }
    //intrinsic motivation inventory questionnaire
    else if ($current_step == "background") {
        //save the participant
        saveBackground($_POST);
    }
}

//redirect
$_SESSION['step'] = $next_step;
header('Location: ' . $next_step . '.php');
?>
